home *** CD-ROM | disk | FTP | other *** search
/ Robotics & Artificial Int…3 (Professional Edition) / Robotics & Artificial Intelligence Tools 2003 (Professional Edition).iso / neural network tool and application / nsinstall.exe / data1.cab / CodeGen_Files / EXAMPLES / XOR1.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  2002-03-08  |  4.1 KB  |  137 lines

  1. // Xor1Temp.cpp
  2. // Automatically generated from breadboard by NeuroSolutions.
  3.  
  4. #include "NSLib.h"      
  5.  
  6.  
  7. void saveAllWeights(char *weightsFilePath);
  8. void file35Access(NSFloat *, int, int);
  9. void dataWriterAccess(NSFloat *, int, int);
  10. BOOL networkStopped=FALSE;
  11. unsigned short int weightFileVersion=100;
  12.  
  13. // Component Construction
  14.     Axon axon1;
  15.     FunctionIO file35;
  16.     FullSynapse fullSynapse2;
  17.     TanhAxon tanhAxon6;
  18.     FullSynapse fullSynapse10;
  19.     TanhAxon tanhAxon14;
  20.     FunctionIO dataWriter;
  21.  
  22. int main() {
  23.     srand((unsigned)time(NULL));
  24.  
  25.     // Component Initialization
  26.     axon1.setRows(2);
  27.     file35.setFunction((void*)file35Access);
  28.     file35.setMode(READ);
  29.     file35.setSpatialDimension(2,1);
  30.     tanhAxon6.setRows(4);
  31.     tanhAxon14.setRows(1);
  32.     dataWriter.setFunction((void*)dataWriterAccess);
  33.     dataWriter.setMode(WRITE);
  34.     dataWriter.setSpatialDimension(1,1);
  35.     FILE *loadStream = fopen("Xor.nsw","r");
  36.     if (!loadStream) {
  37.         fprintf(stderr, "Could not open weight file Xor.nsw");
  38.         exit(1);
  39.     }
  40.     weightFileVersion = getWeightFileVersion(loadStream);
  41.  
  42.     // Load Normalization Coefficients of Files
  43.     file35.loadWeights(seekComponent(loadStream, "File", "file35"),weightFileVersion);
  44.  
  45.     // Component Interconnection
  46.     axon1.setPreActivityAccess(&file35);
  47.     tanhAxon14.setActivityAccess(&dataWriter);
  48.     axon1.setNext(&fullSynapse2);
  49.     fullSynapse2.setLast(&axon1);
  50.     fullSynapse2.setNext(&tanhAxon6);
  51.     tanhAxon6.setLast(&fullSynapse2);
  52.     tanhAxon6.setNext(&fullSynapse10);
  53.     fullSynapse10.setLast(&tanhAxon6);
  54.     fullSynapse10.setNext(&tanhAxon14);
  55.     tanhAxon14.setLast(&fullSynapse10);
  56.  
  57.     // Load Axon Weights
  58.     axon1.loadWeights(seekComponent(loadStream, "Axon", "axon1"),weightFileVersion);
  59.      tanhAxon6.loadWeights(seekComponent(loadStream, "TanhAxon", "tanhAxon6"),weightFileVersion);
  60.      tanhAxon14.loadWeights(seekComponent(loadStream, "TanhAxon", "tanhAxon14"),weightFileVersion);
  61.  
  62.     // Load Synapse Weights
  63.     fullSynapse2.loadWeights(seekComponent(loadStream, "FullSynapse", "fullSynapse2"),weightFileVersion);
  64.     fullSynapse10.loadWeights(seekComponent(loadStream, "FullSynapse", "fullSynapse10"),weightFileVersion);
  65.     fclose(loadStream);
  66.  
  67.     // Get Ready to Run Network
  68.  
  69.     // Run Network
  70.     for (unsigned int epoch=0; epoch<100; epoch++) { 
  71.         for (unsigned int exemplar=0; exemplar<4; exemplar++) { 
  72.             axon1.fire();
  73.             if (networkStopped)
  74.                 goto ConcludeFiring;
  75.         }
  76.     }
  77.     
  78.     ConcludeFiring:
  79.     saveAllWeights("Xor1Temp.nsw");
  80.  
  81.     return 0;
  82. }
  83.  
  84. void saveAllWeights(char *weightsFilePath)
  85. {    FILE *saveStream = fopen(weightsFilePath,"w");
  86.     if (!saveStream) {
  87.         fprintf(stderr, "Could not open weight file %s\n", weightsFilePath);
  88.         exit(1);
  89.     }
  90.     fprintf(saveStream,"#NSWeightFileVersion 135\n\n");
  91.     file35.saveWeights(putComponent(saveStream, "File", "file35"));
  92.      axon1.saveWeights(putComponent(saveStream, "Axon", "axon1"));
  93.      fullSynapse2.saveWeights(putComponent(saveStream, "FullSynapse", "fullSynapse2"));
  94.      tanhAxon6.saveWeights(putComponent(saveStream, "TanhAxon", "tanhAxon6"));
  95.      fullSynapse10.saveWeights(putComponent(saveStream, "FullSynapse", "fullSynapse10"));
  96.      tanhAxon14.saveWeights(putComponent(saveStream, "TanhAxon", "tanhAxon14"));
  97.      fclose(saveStream);
  98. }
  99.  
  100. void file35Access(NSFloat *data, int rows, int cols) {
  101.     // This function is called each time the axon1's Pre-Activity data changes
  102.     // This function's code was written after NeuroSolutions generated the code for the BB
  103.     // (Begin)
  104.     int     length,
  105.         i;
  106.     char    response[128];
  107.  
  108.     length = rows*cols;
  109.     printf("Enter the space-delimited data samples ('exit' to quit)):\n");
  110.     scanf("%s",response);
  111.     if (strcmp(response,"exit") != 0) {
  112.         sscanf(response,"%f", data);
  113.         for (i=1; i<length; i++)
  114.             scanf("%f", data+i);
  115.     }
  116.     else
  117.         networkStopped=TRUE;
  118.     // (End)
  119. }
  120.  
  121. void dataWriterAccess(NSFloat *data, int rows, int cols) {
  122.     // This function is called each time the tanhAxon14's Activity data should be changed
  123.     // This function's code was written after NeuroSolutions generated the code for the BB
  124.     // (Begin)
  125.     int     j,
  126.         i;
  127.  
  128.     printf("Network output:\n");
  129.     for (i=0; i<rows; i++) {
  130.         for (j=0; j<cols; j++)
  131.             printf("%f ", data[i*cols + j]);
  132.         printf("\n");
  133.     }
  134.     // (End)
  135. }
  136.  
  137.